package com.fy.springmvc2.controller;


import com.fy.springmvc2.entity.User;
import com.fy.springmvc2.service.intf.UserService;
import com.fy.springmvc2.vo.R;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

@RestController
@SessionAttributes({"user"})
public class LoginController {
    @Autowired
    private UserService service;

    @RequestMapping("/login")
    public String login(User user, Model model, String code, HttpServletRequest request) {
        //获取session中存在的验证码
        String captcha = (String) request.getSession().getAttribute("captcha");
        //比对验证码
        if (Objects.equals(captcha, code)) {
            //调用业务层进行登陆
            User login = service.login(user.getName(), user.getPassword());
            //如果返回的用户不是nuLl 说明登录成功
            if (Objects.nonNull(login)) {
                //登陆成功
                model.addAttribute("user", user);
                //查询所有用户 返回到页面进行显示
                PageInfo<User> users = service.selectPage(1, 2);
                model.addAttribute("pageInfo", users);
                return "welcome";
            } else {
                // 登录失败
                model.addAttribute("error", "用户名或密码错误");
            }
        } else {
            // 验证码错误
            model.addAttribute("error", "验证码错误");
        }
        return "index";
    }


//    @RequestMapping("/loginJson")
//    @ResponseBody
//    public String loginJson(User user, Model model, String code, HttpServletRequest request) {
//        //获取session中存在的验证码
//        String captcha = (String) request.getSession().getAttribute("captcha");
//        System.out.println(captcha);
//        System.out.println(code);
//        System.out.println(user);
//        //比对验证码
//        if (Objects.equals(captcha, code)) {
//            //调用业务层进行登陆
//            User login = service.login(user.getName(), user.getPassword());
//            // 如果返回的用户不是null 说明登录成功
//            if (Objects.nonNull(login)) {
//                return "{\"code\":0,\"msg\":\"登录成功\"}";
//            } else {
//                return "{\"code\":-1,\"msg\":\"用户名或密码错误\"}";
//            }
//        } else {
//            // 验证码输入错误
//            return "{\"code\":-1,\"msg\":\"验证码错误\"}";
//        }
//    }

    @RequestMapping("/loginJson")
    public R<User> loginJson( User user, Model model, String code, HttpServletRequest request) {
        //获取session中存在的验证码
        String captcha = (String) request.getSession().getAttribute("captcha");
        System.out.println(captcha);
        System.out.println(code);
        System.out.println(user);
        //比对验证码
        if (Objects.equals(captcha, code)) {
            //调用业务层进行登陆
            User loginUser = service.login(user.getName(), user.getPassword());
            // 如果返回的用户不是null 说明登录成功
            if (Objects.nonNull(loginUser)) {
                //前端想获取登录人的信息
                return R.ok(0,"登陆成功",loginUser);
            } else {
                return R.fail(-1,"账户或密码错误",null);
            }
        } else {
            // 验证码输入错误
            return R.fail(-1,"验证码错误",null);
        }
    }
}

